package com.mybatiscore.jiaocheng.d04mybatis动态Sql;

import com.mybatiscore.Mapper.StudentMapper;
import com.mybatiscore.POJO.Student;
import com.mybatiscore.Util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class d06_foreach {
    /*
       foreach标签:
            collection:指定传进来的数组或集合
            item：其中的元素
            separator:循环的分隔符
            open:语句的开始
            close:结束的最后面的结尾
     */
    //批量删除
    @Test
    public void testDeleteForeach(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int i = mapper.deleteByIds(new Long[]{1L, 2L});
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();
    }

    //批量添加
    @Test
    public void testInsertForeach(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student1=new Student(null,"dkdk",18,1.67,new Date(),'男');
        Student student2=new Student(null,"dkd",18,1.67,new Date(),'女');
        Student student3=new Student(null,"dkk",20,1.67,new Date(),'男');
        List<Student> lists=new ArrayList<Student>();
        lists.add(student1);
        lists.add(student2);
        lists.add(student3);
        int i = mapper.insertByIds(lists);
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();

    }
}
